{extend name="common/base"}
{block name="content"}
<div class="layui-container">
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">类型名称</label>
            <div class="layui-input-block">
                <input value="{$type.name}" type="text" name="name" required lay-verify="required" placeholder="请填写合同类型名称" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属分类</label>
            <div class="layui-input-block">
                <select name="category_id" required lay-verify="required" lay-search lay-filter="category">
                    <option value>请输入选择</option>
                    {volist name="categories" id="category"}
                    <option value="{$category.id}" {if $type.category_id == $category.id}selected="selected"{/if}>{$category.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input value="{$type.sn}" type="text" name="sn" required lay-verify="required" placeholder="合同类型编号" autocomplete="off"
                       class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="note" placeholder="请输入内容" class="layui-textarea">{$type.note}</textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="location.href='/type/all'">返回</button>
            </div>
        </div>
    </form>
</div>
{/block}

{block name="js"}
{__block__}
<script>
    layui.use(['form'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        // 监听类型
        form.on('select(category)', function(data){
            console.log(data.elem); //得到select原始DOM对象
            console.log(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象
            // 当前分类，sn号不变，否则获取sn号
            if (data.value == '{$type.category_id}') {
                $('input[name="sn"]').val('{$type.sn}');
            } else {
                var snLoader = layer.load(1);
                $.get({
                    url: '/type/getNextSn/' + data.value,
                    success: function (result) {
                        layer.close(snLoader);
                        if (result == -1) {
                            layer.open({content:'获取类型编号失败！', icon:2});
                        } else {
                            $('input[name="sn"]').val(result);
                        }
                    },
                    error: function (error) {
                        layer.close(snLoader);
                        console.log(error)
                        layer.open({content:'内部错误！', icon:2});
                    }
                });
            }
        });
        //监听提交
        form.on('submit(formDemo)', function(data){
            var index1 = layer.load(1);
            console.log(data.field)
            $.post({
                type: 'POST',
                url: '/type/edit/{$type.id}',
                data: data.field,
                success: function (result) {
                    console.log(result);
                    layer.close(index1);
                    layer.msg('修改成功');
                },
                error: function (error) {
                    layer.close(index1);
                    console.log(error);
                    layer.msg('修改失败');
                }
            });
            return false;
        });
    });

</script>
{/block}